package com.neegix.system.role.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import com.neegix.system.role.application.assembler.RoleAssembler;
import com.neegix.system.role.application.cqrs.query.RoleQueryRepository;
import com.neegix.system.role.application.cqrs.query.condition.RoleWhereGroup;
import com.neegix.system.role.application.dto.RoleDTO;
import com.neegix.system.role.infrastructure.repository.convert.RoleConverter;
import com.neegix.system.role.infrastructure.repository.dataobject.RoleDO;
import com.neegix.system.role.infrastructure.repository.dataobject.RoleMenuRelDO;
import com.neegix.system.role.infrastructure.repository.mapper.RoleMapper;
import com.neegix.system.role.infrastructure.repository.mapper.customized.RoleCustomizedMapper;
import com.neegix.system.role.interfaces.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-25 11:32:40
  */

@Repository
public class RoleQueryRepositoryImpl implements RoleQueryRepository {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleCustomizedMapper roleCustomizedMapper;

    @Override
    public Optional<RoleVO> findById(Long id) {
        RoleDO roleDO = roleMapper.selectByPrimaryKey(id);
        RoleVO roleVO = RoleAssembler.INSTANCE.covertVO(RoleConverter.INSTANCE.covertDTO(roleDO));
        List<Long> apiPermissions = roleCustomizedMapper.getApiPermissionsByPkRole(id);

        List<RoleMenuRelDO> pagePermissions = roleCustomizedMapper.getMenuPermissionsByPkRole(id);
        roleVO.setApiPermissions(apiPermissions);
        roleVO.setPagePermissions(RoleAssembler.INSTANCE.covertPagePermissionVO(RoleConverter.INSTANCE.covertDTO(pagePermissions)));
        return Optional.of(roleVO);
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return roleCustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<RoleVO> findPage(Integer currentPage, Integer pageSize, RoleDTO roleDTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(RoleWhereGroup.class)
           .andCreateTimeBetween(roleDTO.getStartCreateTime(),roleDTO.getEndCreateTime())
           .andCreateUserEqualTo(roleDTO.getCreateUser())
           .andDeletedEqualTo(roleDTO.getDeleted())
           .andEnabledEqualTo(roleDTO.getEnabled())
           .andIdEqualTo(roleDTO.getId())
           .andNameLikeTo(roleDTO.getName())
           .andDescriptionLikeTo(roleDTO.getDescription())
           .andUpdateTimeBetween(roleDTO.getStartUpdateTime(),roleDTO.getEndUpdateTime())
           .andUpdateUserEqualTo(roleDTO.getUpdateUser());
           
        nebulaSQL.setPager(currentPage, pageSize);
        List<RoleDO> result = roleMapper.selectList(nebulaSQL);
        Long total = roleMapper.selectCount(nebulaSQL);
        PageVO<RoleVO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(RoleAssembler.INSTANCE.covertVO(RoleConverter.INSTANCE.covertDTOS(result)));
        return page;
    }

     @Override
     public List<RoleVO> findAll() {
         NebulaSQL nebulaSQL = new NebulaSQL();
         return RoleAssembler.INSTANCE.covertVO(RoleConverter.INSTANCE.covertDTOS(roleMapper.selectList(nebulaSQL)));
     }
 }
